<template>
	<view class="note-detail">
		<view class="note-detail-wrap">
			<view class="video" v-if="noteDetailInfo.video_url">
				<video :src="$util.img(noteDetailInfo.video_url)" :unit-id="noteDetailInfo.videofront" controls objectFit="cover"></video>
			</view>
			<!--顶部-->
			<view class="note-detail-top u-p-30">
				<view class="title u-font-40 u-line-2">{{noteDetailInfo.note_title}}</view>
				<!--阅读数、发布时间信息-->
				<view class="note-detail-info u-flex">
					<view class="info-time u-tips-color">
						<text>发布时间：</text>
						{{ $util.timeStampTurnTime(noteDetailInfo.create_time, 'time') }}
					</view>
					<view class="info-views u-m-l-25 u-tips-color">
						<u-icon name="eye" siz="28" color="#909399"></u-icon>
						<text class="u-m-l-5">{{ noteDetailInfo.initial_read_num + noteDetailInfo.read_num }}</text>
					</view>
					<view class="info-like u-m-l-10 u-tips-color">
						<u-icon name="heart" size="28" color="#909399"></u-icon>
						<text
							class="u-m-l-5">{{ noteDetailInfo.initial_dianzan_num + noteDetailInfo.dianzan_num }}</text>
					</view>
				</view>
			</view>
			<!--内容-->
			<view class="note-detail-content u-p-30">
				<!--小程序流量主广告-->
				<!-- #ifdef MP -->
				<view v-if="noteDetailInfo.wx_banner_first !==''">
					<ad :unit-id="noteDetailInfo.wx_banner_first" ad-type="banner"></ad>
				</view>
				<view class="u-m-t-20" v-if="noteDetailInfo.wx_banner_first !==''">
					<ad :unit-id="noteDetailInfo.wx_banner_second" ad-type="banner"></ad>
				</view>
				<!-- #endif -->

				<!--食谱-->
				<view class="recipe-item" v-if="noteDetailInfo.note_type=='recipe_item'">
					<view class="cook-abstract u-m-b-40" v-if="noteDetailInfo.note_abstract !==''">
						<view class="sub-title u-m-b-40">
							<u-tag text="食谱故事" mode="dark" shape="circle" size="mini" bg-color="#333" />
						</view>
						<view class="tips-bg">
							{{noteDetailInfo.note_abstract}}
						</view>
					</view>

					<view class="coook-time u-m-b-40" v-if="noteDetailInfo.cooktime!==''">
						<u-tag :text="'烹饪时间：'+noteDetailInfo.cooktime + '分钟'" mode="dark" shape="circle"
							bg-color="#333" />
					</view>

					<view class="cook-list u-m-b-40" v-if="noteDetailInfo.shicai.length">
						<view class="sub-title u-m-b-40">
							<u-tag text="食材清单" mode="dark" size="mini" shape="circle" bg-color="#333"></u-tag>
						</view>
						<view class="u-border-bottom" v-for="(item, index) in noteDetailInfo.shicai" :key="index">
							{{item}}
						</view>
					</view>

					<view class="recipe-item-cooktips u-m-b-40" v-if="noteDetailInfo.cook_tips !=='' ">
						<view class="sub-title u-m-b-40">
							<u-tag text="小贴士" mode="dark" shape="circle" size="mini" bg-color="#333" />
						</view>
						<view class="tips-bg">
							{{noteDetailInfo.cook_tips}}
						</view>
					</view>

					<!--关联商品-->
					<view class="sub-title u-m-b-40" v-if="noteDetailInfo.goods_list.length">
						<u-tag text="购买食材" mode="dark" shape="circle" size="mini" bg-color="#333" />
					</view>
					<view v-if="noteDetailInfo.goods_list" v-for="(goodsItme, goodsIndex) in noteDetailInfo.goods_list"
						:key="goodsIndex" @click="redirectToGoods(goodsItme.sku_id, 'shop_said')">
						<view class="goods-card-body u-border">

							<view class="goods-card-item u-flex">
								<image class="goods-card-cover" :src="$util.img(goodsItme.goods_image.split(',')[0])"
									:lazy-load="true" mode="aspectFill"></image>
								<view class="u-text-left u-m-l-20">
									<view class="title">{{ goodsItme.goods_name }}</view>
									<view class="price">￥{{ goodsItme.price }}</view>
								</view>
							</view>
						</view>
					</view>
				</view>
				<view class="u-m-t-40">
					<view class="sub-title u-m-b-40" v-if="noteDetailInfo.note_type == 'recipe_item' ">
						<u-tag text="烹饪步骤" mode="dark" shape="circle" size="mini" bg-color="#333" />
					</view>
					<rich-text :nodes="noteDetailInfo.note_content"></rich-text>
				</view>
				<view class="said-action">
					<u-icon name="thumb-up" size="28" @click="giveLike" v-if="!giveLikeIdent"></u-icon>
					<u-icon name="thumb-up-fill" size="28" @click="giveLike" v-if="giveLikeIdent"></u-icon>
					<!-- #ifdef MP -->

					<button @click="openSharePopup()">
						<u-icon name="share" color="#FFF"></u-icon>
					</button>
					<!-- #endif -->
				</view>
			</view>

			<!-- 海报 -->
			<view @touchmove.prevent.stop>
				<uni-popup ref="posterPopup" type="bottom" class="poster-layer">
					<template v-if="poster != '-1'">
						<view :style="{ height: posterHeight > 0 ? posterHeight + 80 + 'px' : '' }">
							<view class="image-wrap">
								<image :src="$util.img(poster)" :show-menu-by-longpress="true"
									:style="{ height: posterHeight > 0 ? posterHeight + 'px' : '' }" />
							</view>
							<!-- #ifdef MP || APP-PLUS  -->
							<view class="save" @click="saveNotesPoster()">保存图片</view>
							<!-- #endif -->
							<!-- #ifdef H5 -->
							<view class="save">长按保存图片</view>
							<!-- #endif -->
						</view>
						<view class="close iconfont iconclose" @click="closePosterPopup()"></view>
					</template>
					<view v-else class="msg">{{ posterMsg }}</view>
				</uni-popup>
			</view>

			<!-- 分享弹窗 -->
			<view @touchmove.prevent.stop>
				<uni-popup ref="sharePopup" type="bottom" class="share-popup">
					<view>
						<view class="share-title">分享</view>
						<view class="share-content">
							<!-- #ifdef MP -->
							<view class="share-box">
								<button class="share-btn" :plain="true" open-type="share">
									<view class="iconfont iconiconfenxianggeihaoyou"></view>
									<text>分享给好友</text>
								</button>
							</view>
							<!-- #endif -->

							<view class="share-box" @click="openPosterPopup">
								<button class="share-btn" :plain="true">
									<view class="iconfont iconpengyouquan"></view>
									<text>生成分享海报</text>
								</button>
							</view>
							<!-- #ifdef H5 -->
							<view class="share-box" @click="copyUrl">
								<button class="share-btn" :plain="true">
									<view class="iconfont iconfuzhilianjie"></view>
									<text>复制链接</text>
								</button>
							</view>
							<!-- #endif -->
						</view>
						<view class="share-footer" @click="closeSharePopup"><text>取消分享</text></view>
					</view>
				</uni-popup>
			</view>
		</view>

	</view>
</template>

<script>
	import htmlParser from '@/common/js/html-parser';
	import globalConfig from '@/common/js/golbalConfig.js';
	import uniPopup from '@/components/uni-popup/uni-popup.vue';
	import {
		Weixin
	} from '@/common/js/wx-jssdk.js';
	export default {
		components: {
			uniPopup
		},
		data() {
			return {
				noteId: '',
				noteDetailInfo: {},
				giveLikeIdent: false,
				giveLikeFlag: false,
				token: "",
				isIphoneX: false, //判断手机是否是iphoneX以上
				poster: "-1", //海报
				posterMsg: "", //海报错误信息
				posterHeight: 0,
			};
		},

		onLoad(options) {
			this.isIphoneX = this.$util.uniappIsIPhoneX()
			if (!this.addonIsExit.notes) {
				this.$util.showToast({
					title: '商家未开启店铺笔记',
					mask: true,
					duration: 2000
				});
				setTimeout(() => {
					this.$util.redirectTo('/pages/index/index/index', {}, 'redirectTo');
				}, 2000);
				return;
			}
			if (options.note_id) {
				this.noteId = options.note_id;
				this.getNoteDetail();
			} else {
				this.$util.redirectTo('/otherpages/store_notes/note_list/note_list', {}, 'redirectTo');
			}
			// 小程序扫码进入
			if (options.scene) {
				var sceneParams = decodeURIComponent(options.scene);
				sceneParams = sceneParams.split('&');
				if (sceneParams.length) {
					sceneParams.forEach(item => {
						if (item.indexOf('note_id') != -1) this.noteId = item.split('-')[1];
						if (item.indexOf('source_member') != -1) uni.setStorageSync('source_member', item.split(
							'-')[1]);
					});
				}
			}
		},
		onShow() {
			// 刷新多语言
			this.$langConfig.refresh();

			if (uni.getStorageSync('token')) {
				this.isDianzan();
				this.getMemberId();
			}
		},
		mixins: [globalConfig],
		onShareAppMessage() {
			var title = this.noteType == 'goods_item' ? this.goodsItemInfo.note_title : this.shopSaidInfo.note_title;
			return {
				title: title,
				path: '/otherpages/store_notes/note_detail/note_detail?note_id=' + this.noteId
			};
		},
		methods: {
			//笔记详情
			getNoteDetail() {
				this.$api.sendRequest({
					url: '/notes/api/notes/detail',
					data: {
						note_id: this.noteId
					},
					success: res => {
						if (res.code == 0) {
							this.noteDetailInfo = res.data;
							this.noteDetailInfo.shicai = this.noteDetailInfo.shicai.split(',');
							this.noteDetailInfo.goods_highlights = this.noteDetailInfo.goods_highlights.split(
								',');
						} else {
							this.$util.showToast({
								title: res.message
							});
						}

						if (this.$refs.loadingCover) this.$refs.loadingCover.hide();
					}
				});
			},
			/* 点赞 */
			giveLike() {
				if (!uni.getStorageSync('token')) {
					this.$refs.login.open('/pages/index/index/index');
					return;
				}

				if (this.giveLikeFlag) return false;
				this.giveLikeFlag = true;

				var url = this.giveLikeIdent ? '/notes/api/record/delete' : '/notes/api/record/add';
				this.$api.sendRequest({
					url: url,
					data: {
						note_id: this.noteId
					},
					success: res => {
						this.giveLikeFlag = false;
						if (res.code == 0 && res.data > 0) {
							this.noteDetailInfo.dianzan_num = this.giveLikeIdent ? this.noteDetailInfo
								.dianzan_num - 1 : this.noteDetailInfo.dianzan_num + 1;
							this.giveLikeIdent = !this.giveLikeIdent;
						} else {
							this.$util.showToast({
								title: res.message
							});
						}
					}
				});
			},
			/* 检测是否点赞 */
			isDianzan() {
				this.$api.sendRequest({
					url: '/notes/api/record/isDianzan',
					data: {
						note_id: this.noteId
					},
					success: res => {
						if (res.code == 0) {
							this.giveLikeIdent = res.data == 1 ? true : false;
						} else {
							this.$util.showToast({
								title: res.message
							});
						}
					}
				});
			},
			/* 关联商品跳转 */
			redirectToGoods(data, type = '') {
				var id = type ? data : data.goods_list[0].sku_id;

				this.$util.redirectTo('/detail/detail', {
					sku_id: id
				});
			},
			imageError() {
				if (this.noteDetailInfo.cover_img) this.noteDetailInfo.cover_img = this.$util.getDefaultImage()
					.default_cover_img;
				this.$forceUpdate();
			},
			//-------------------------------------分享-------------------------------------
			// 打开分享弹出层
			openSharePopup() {
				this.$refs.sharePopup.open();
			},
			// 关闭分享弹出层
			closeSharePopup() {
				this.$refs.sharePopup.close();
			},
			//-------------------------------------海报-------------------------------------

			// 打开海报弹出层
			openPosterPopup() {
				this.getNotesPoster();
				this.$refs.sharePopup.close();
				this.$refs.posterPopup.open();
				if (this.poster != '-1') {
					setTimeout(() => {
						let view = uni.createSelectorQuery().in(this).select(".poster-layer .image-wrap");
						view.fields({
							size: true,
						}, data => {
							let posterWhith = data.width;
							let ratio = parseFloat((740 / posterWhith).toFixed(2));
							if (this.token != '') {
								this.posterHeight = parseInt(1120 / ratio);
							} else {
								this.posterHeight = parseInt(1100 / ratio);
							}
						}).exec();
					}, 100);
				}
			},
			// 关闭海报弹出层
			closePosterPopup() {
				this.$refs.posterPopup.close();
			},
			//生成海报
			getNotesPoster() {
				//活动海报信息
				let qrcode_param = {
					note_id: this.noteId
				};
				if (this.memberId) qrcode_param.source_member = this.memberId;

				this.$api.sendRequest({
					url: "/notes/api/notes/poster",
					data: {
						page: '/otherpages/store_notes/note_detail/note_detail',
						qrcode_param: JSON.stringify(qrcode_param)

					},
					success: res => {
						if (res.code == 0) {
							this.poster = res.data.path + "?time=" + new Date().getTime();
						} else {
							this.posterMsg = res.message;
						}
					}
				});
			},
			// 预览图片
			previewMedia(index) {
				var paths = [];
				for (let i = 0; i < this.recipeItemInfo.cover_img.length; i++) {
					paths.push(this.$util.img(this.recipeItemInfo.cover_img[i], {
						size: 'big'
					}));
				}
				uni.previewImage({
					current: index,
					urls: paths,
				});
			},
			// #ifdef MP || APP-PLUS
			//小程序中保存海报
			saveNotesPoster() {

				let url = this.$util.img(this.poster);

				uni.downloadFile({
					url: url,
					success: (res) => {
						if (res.statusCode === 200) {
							uni.saveImageToPhotosAlbum({
								filePath: res.tempFilePath,
								success: () => {
									this.$util.showToast({
										title: "保存成功"
									});
								},
								fail: () => {
									this.$util.showToast({
										title: "保存失败，请稍后重试"
									});
								}
							});
						}
					},

					fail: (e) => {

					}
				});
			},
			// #endif
			//获取用户信息
			getMemberId() {
				this.$api.sendRequest({
					url: '/api/member/id',
					success: res => {
						if (res.code >= 0) {
							this.memberId = res.data;
							this.token = uni.getStorageSync('token');
						}
						if (this.$refs.loadingCover) this.$refs.loadingCover.hide();
					}
				});
			},

		}
	};
</script>

<style lang="scss">
	page {
		background: #FFF;
	}

	.item-action {
		.action-left {
			display: flex;

			text {
				&.active {
					margin: 0;
					background-color: rgb(255, 255, 255) !important;
					border: 2rpx solid #dddddd;
				}

				display: flex;
				justify-content: center;
				align-items: center;
				width: 70rpx;
				height: 70rpx;
				background-color: rgba(0, 0, 0, 0.4) !important;
				border-radius: 50%;
				border: 2rpx solid rgba(0, 0, 0, 0);
			}

			button {
				display: flex;
				justify-content: center;
				align-items: center;
				margin: 0;
				margin-left: 16rpx;
				padding: 0;
				width: 70rpx;
				height: 70rpx;
				background-color: rgba(0, 0, 0, 0.4) !important;
				border-radius: 50%;
			}

			.iconfont {
				color: #fff;
				font-size: $font-size-toolbar;
			}
		}

		.action-right {
			margin: 0;
			width: 180rpx;
			height: 70rpx;
			color: #fff;
		}

		position: fixed;
		bottom: 0;
		width: calc(100% - 24px);
		display: flex;
		margin: 80rpx 0;
		justify-content: space-between;
	}

	.said-action {
		position: fixed;
		bottom: 20rpx;
		width: calc(100% - 24px);
		display: flex;
		justify-content: center;
		margin: 80rpx 0;

		text {
			&.active {
				margin: 0;
				background-color: rgb(255, 255, 255) !important;
				border: 2rpx solid #dddddd;
			}

			display: flex;
			justify-content: center;
			align-items: center;
			width: 70rpx;
			height: 70rpx;
			background-color: rgba(0, 0, 0, 0.4) !important;
			border-radius: 50%;
		}

		button {
			display: flex;
			justify-content: center;
			align-items: center;
			margin: 0;
			margin-left: 16rpx;
			padding: 0;
			width: 70rpx;
			height: 70rpx;
			background-color: rgba(0, 0, 0, 0.4) !important;
			border-radius: 50%;
		}

		.iconfont {
			color: #fff;
			font-size: $font-size-toolbar;
		}
	}

	.goods-card-cover {
		width: 180rpx;
		height: 180rpx;
	}

	.price {
		margin-top: 35rpx;
		color: red;
	}

	.tips-bg {
		background: #f3f4f6;
		padding: 20rpx;
		border-radius: 10rpx;
	}

	video {
		width: 100% !important;

	}

	.share-popup,
	.uni-popup__wrapper-box {
		.share-title {
			line-height: 60rpx;
			font-size: $font-size-toolbar;
			padding: 15rpx 0;
			text-align: center;
		}

		.share-content {
			display: flex;
			display: -webkit-flex;
			-webkit-flex-wrap: wrap;
			-moz-flex-wrap: wrap;
			-ms-flex-wrap: wrap;
			-o-flex-wrap: wrap;
			flex-wrap: wrap;
			padding: 80rpx 15rpx;

			.share-box {
				flex: 1;
				text-align: center;

				.share-btn {
					margin: 0;
					padding: 0;
					border: none;
					line-height: 1;
					height: auto;

					text {
						margin-top: 20rpx;
						font-size: $font-size-tag;
						display: block;
						color: $color-title;
					}
				}

				.iconfont {
					font-size: 80rpx;
					line-height: initial;
				}

				.iconfuzhilianjie,
				.iconpengyouquan,
				.iconhaowuquan,
				.iconiconfenxianggeihaoyou {
					color: #07c160;
				}
			}
		}

		.share-footer {
			height: 90rpx;
			line-height: 90rpx;
			border-top: 2rpx solid $color-line;
			text-align: center;
		}
	}

	// 海报
	// .uni-popup__wrapper-box
	.poster-layer {
		.generate-poster {
			padding: 40rpx 0;

			.iconfont {
				font-size: 80rpx;
				color: #07c160;
				line-height: initial;
			}

			>view {
				text-align: center;

				&:last-child {
					margin-top: 20rpx;
				}
			}
		}

		.image-wrap {
			width: 70%;
			margin: 30px auto 20px auto;
			box-shadow: 0 0 16px rgba(100, 100, 100, 0.3);

			image {
				width: 100%;
				height: 100%;
				height: 750rpx;
			}
		}

		.msg {
			padding: 40rpx;
		}

		.save {
			text-align: center;
			height: 80rpx;
			line-height: 80rpx;
		}

		.close {
			position: absolute;
			top: 0;
			right: 20rpx;
			width: 40rpx;
			height: 80rpx;
			font-size: 50rpx;
		}
	}
</style>
